%w(active_support mp3info).each{|f| require(f);};

class String
  
  def remove(*substs)
    substs.flatten.inject(self){|str, subst| str.gsub(subst, "");};
  end
  
  alias_method :-, :remove;

  def sanitize
    self.downcase.chomp.remove(/\(.*?\)?/, /\s?(prod|feat|ft\.|featuring|remastered).*/).titleize.gsub(/(\w|\d)'(\w+)\s/){|match| "#{$1}'#{$2.downcase} ";};
  end

end

# Change below
$tags = [:title, {:album => "Sean Kingston"}, {:artist => "Sean Kingston"}];
$subfolders = false;
$folder = 'C:\Users\Rufino\Downloads\P2P\Sean Kingston - Sean Kingston [2007][CD+SkidVid+Cov]192Kbps';
# Stop change

Dir.chdir($folder);
Songs = [];

Dir.glob(($subfolders) ? "*/*.mp3" : "*.mp3").each do |mp3|
  if $subfolders
    path, mp3 = mp3.split("/");
    Dir.chdir(path);
  end
  mp3 = Mp3Info.new(mp3);
  Songs << mp3;
  
  filename = "%artist - %title.mp3";
  
  puts "\n--";
  
  $tags.each do |tag|
    value = (tag.kind_of?(Hash)) ? tag.to_a.flatten[1] : mp3.tag.send(tag).sanitize;
    tag = (tag.kind_of?(Hash)) ? tag.to_a.flatten[0] : tag;
    mp3.tag.send("#{tag}=", value);
    puts "#{tag.to_s.titleize}: #{value.inspect}";
  end
  
  mp3.close;
  
  mp3.tag.each do |tag, value|
    filename.gsub!("%#{tag}", value.to_s);
  end
  
  File.rename(mp3.filename, filename);
  puts "Renamed: #{filename.inspect}";

  puts "--\n";
  
  
  Dir.chdir "../" if $subfolders;
end

puts "#{Songs.size} renamed"